package com.gx.aut.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gx.aut.common.exception.BusinessException;
import com.gx.aut.entity.User;
import com.gx.aut.mapper.UserMapper;
import com.gx.aut.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;


    @Override
    public void register(User user) {
        User userOne = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUserName, user.getUserName()));
        if (userOne != null){
            throw new BusinessException("用户已存在，请前往直接登录");
        }
        userMapper.insert(user);
    }

    @Override
    public void addUser(User user) {
        User userOne = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUserName, user.getUserName()));
        if (userOne != null){
            throw new BusinessException("用户已存在，请勿重复添加");
        }
        userMapper.insert(user);
    }

    @Override
    public List<User> findAllUser() {
        return userMapper.selectList(null);
    }
}
